import json
from datetime import datetime, timedelta
import random
from bson import ObjectId


def generate_data(count=1000):
    data = []
    base_data = {
        "thumb": "https://dl.pvp.xoyo.com/prod/icons/System/Pack/Sc_koudao01.png?v=2",
        "tags": [],
        "state": 5,
        "single_unit_size": 1,
        "remain_unit_count": 1,
        "zone_id": "z05",
        "zone_name": "电信区",
        "server_id": "gate0502",
        "server_name": "梦江南",
        "seller_role_name": "阮***",
        "is_followed": 0,
        "followed_num": 5,
        "is_new": 1,
        "remaining_time": 854082,
        "info": "椿山漫·江南·标准",
        "account_type": 0
    }

    # 生成基准时间
    base_time = datetime.now()

    for i in range(count):
        new_data = base_data.copy()

        # 生成唯一_id
        new_data["_id"] = {"$oid": str(ObjectId())}

        # 生成attrs
        new_data["attrs"] = {
            "item_index": 50000 + i,  # 确保不同
            "item_type": random.randint(1, 5),
            "appearance_type_name": "外观礼盒",
            "appearance_point_rewards": ""
        }

        # 生成价格 (10000-99999)
        new_data["single_unit_price"] = random.randint(10000, 99999)

        # 生成consignment_id
        new_data["consignment_id"] = str(1184155464075399168 + i)

        # 生成时间
        create_time = base_time - timedelta(minutes=random.randint(0, 1440))  # 随机1天内的时间
        update_time = create_time + timedelta(minutes=random.randint(0, 180))  # 创建时间后的3小时内

        new_data["createTime"] = create_time.strftime("%Y-%m-%d %H:%M:%S")
        new_data["updateTime"] = update_time.strftime("%Y-%m-%d %H:%M:%S")

        data.append(new_data)

    return data


# 生成数据
data = generate_data(1000)

# 写入文件
with open('mongodb_import_data.json', 'w', encoding='utf-8') as f:
    # 每行写入一个文档，便于mongoimport导入
    for doc in data:
        f.write(json.dumps(doc, ensure_ascii=False) + '\n')